import {Component,ViewChild,ElementRef} from  '@angular/core';
import {NavController} from 'ionic-angular';
import {HttpService} from '../../providers/HttpService';
import {NativeServiceC} from "../../providers/NativeServiceC";
import {GlobalData} from "../../providers/GlobalData";

@Component({
    selector: 'page-shopping',
    templateUrl: 'shopping.html'
})

export class ShoppingPage {

    // @ViewChild('img') img:ElementRef;

    items = [];
    nextUrl:string = '';
    isRefresh:boolean = true;
    constructor(public navCtrl:NavController,
                private nativeService:NativeServiceC,
                private globalData:GlobalData,
                private httpService:HttpService) {
    }

    ionViewDidLoad() {
        let url = '/v2/items';
        this.getShops(url);
    }

    // 加载更多
    doInfinite(event){
        this.getShops(this.nextUrl,{},event);
    }

    private getShops(url,sender?,infiniteScroll?) {
        sender = sender || {
                gender: 1,
                generation: 1,
                limit: 20,
                offset: 0
            };
        if (!this.isRefresh){
            this.globalData.showLoading = false;
        }
        this.httpService.get(url, sender).subscribe(res => {
            this.nativeService.hideLoading();
            if (this.isRefresh) this.items.length = 0;
            this.items = this.items.concat(res.items);
            this.nextUrl = res.paging.next_url;
            this.isRefresh = false;
            infiniteScroll && infiniteScroll.complete();


        })
    }
}

